//
//  WYMediaPriceController.m
//  CirclePush
//
//  Created by admin on 2018/8/4.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYMediaPriceController.h"
#import "WYMdeiaCell.h"
#import "WYPriceModel.h"
#import "WYPriceRangeCell.h"
#import "WYMediaReusableView.h"
@interface WYMediaPriceController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)WYPriceRangeCell *priceRangeCell;
@property (nonatomic,strong)UIButton *sureButton;
@property (nonatomic,strong)NSMutableArray *priceArray;

@end

@implementation WYMediaPriceController
-(NSMutableArray *)priceArray
{
    if (!_priceArray) {
        _priceArray = [[NSMutableArray alloc]init];
    }
    return _priceArray;
}


-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self loadPrice];
    // Do any additional setup after loading the view.
}
-(void)loadPrice
{
    NSArray *array = [WYPriceModel getPriceArray];
    [self.dataArray addObjectsFromArray:array];
    [self.collectionView reloadData];
}
-(void)setupUI
{
    
    UIView *backView = [[UIView alloc]init];
    [self.view addSubview:backView];
    backView.frame = self.view.bounds;
    backView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.2];
    
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.itemSize=CGSizeMake((kScreenWidth-50)/4, 40);
    //横向最小距离
    layout.minimumInteritemSpacing=10.0f;
    layout.minimumLineSpacing=10.0f;
    //cell距离上  左  下 右 的距离
    layout.sectionInset=UIEdgeInsetsMake(10, 10, 10, 10);
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    [layout setHeaderReferenceSize:CGSizeMake(kScreenWidth, 44)];
    
    UICollectionView * collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, backView.height/3*2-50) collectionViewLayout:layout];
    self.collectionView = collection;
    [backView addSubview:collection];
    collection.backgroundColor = [UIColor whiteColor];
    collection.delegate = self;
    collection.dataSource = self;
    
    [collection registerNib:[UINib nibWithNibName:@"WYMediaReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"WYMediaReusableView"];
    
    [collection registerNib:[UINib nibWithNibName:@"WYMdeiaCell" bundle:nil] forCellWithReuseIdentifier:@"WYMdeiaCell"];
    [collection registerNib:[UINib nibWithNibName:@"WYPriceRangeCell" bundle:nil] forCellWithReuseIdentifier:@"WYPriceRangeCell"];
    
    [backView addSubview:self.sureButton];
    
}
#pragma mark - uicollectionViewDatasoure

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 2;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section==0) {
        return self.dataArray.count;
    }else{
        return 1;
    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        WYMdeiaCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WYMdeiaCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"WYMdeiaCell" owner:nil options:nil].firstObject;
        }
        [cell setBorderWithWidth:kLineHeight andColor:[UIColor grayColor]];
        [cell setCornerRadius:5.0];
        WYPriceModel *model  = self.dataArray[indexPath.row];
        cell.textLabel.text = model.name;
        
        if (model.state) {
            [cell setBorderWithWidth:kLineHeight andColor:RGBA(250, 165, 122, 1.0)];
            [cell setBackgroundColor:RGBA(255, 247, 244, 1.0)];
            //attr.state = NO;
        }else{
            [cell setBorderWithWidth:kLineHeight andColor:[UIColor grayColor]];
            [cell setBackgroundColor:[UIColor whiteColor]];
            // attr.state = YES;
        }
        
        
        return cell;
    }
    if (indexPath.section==1) {
        WYPriceRangeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WYPriceRangeCell" forIndexPath:indexPath];
        self.priceRangeCell = cell;
        [cell.leftTextfield addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [cell.rightTextfield addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        self.priceRangeCell = cell;
        
        return cell;
    }
    return nil;
}
-(void)textFieldDidChange:(UITextField *)textField
{
    if (textField.text.length>0) {
//        for (WYPriceModel *model in self.priceArray) {
//            model.state= NO;
//        }
//        [self.collectionView reloadData];
    }
    
    
    
}

-(void)inputPriceinConditionborderAndType:(BOOL)type WithMaxPrice:(NSString *)maxPrice WithMinPrice:(NSString *)minPrice
{
    for (WYPriceModel *model in self.priceArray) {
        model.state= NO;
    }
    [self.collectionView reloadData];
    if (type) {
        if (minPrice.integerValue>maxPrice.integerValue) {
            
            [self toast:@"最大范围不能小于最小范围"];
            
        }
        WYPriceModel *model = [[WYPriceModel alloc]init];
        model.name = [NSString stringWithFormat:@"%@~%@万",minPrice,maxPrice];
        model.minPrice = minPrice;
        model.maxPrice = maxPrice;
        
//        for (NSUInteger i = self.collArray.count; i>0; i--) {
//            if ([self.collArray[i-1] isKindOfClass:[WYPriceModel class]]) {
//                [self.collArray removeObjectAtIndex:i-1];
//            }
//        }
//        [self.collArray addObject:model];
        
        
        
    }else{
        
        WYPriceModel *model = [[WYPriceModel alloc]init];
        if (maxPrice.length) {
            model.name = [NSString stringWithFormat:@"%@万以下",maxPrice];
        }
        if (minPrice.length) {
            model.name = [NSString stringWithFormat:@"%@万以上",minPrice];
        }
        model.minPrice = minPrice;
        model.maxPrice = maxPrice;
        
//        for (NSUInteger i = self.collArray.count; i>0; i--) {
//            if ([self.collArray[i-1] isKindOfClass:[WYPriceModel class]]) {
//                [self.collArray removeObjectAtIndex:i-1];
//            }
//        }
//        [self.collArray addObject:model];
        
        
    }
}







- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;
    if (kind == UICollectionElementKindSectionHeader)
    {
        
        WYMediaReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"WYMediaReusableView" forIndexPath:indexPath];
        if (indexPath.section==0) {
            headerView.textLabel.text=@"选择刊例价";
        }else{
            headerView.textLabel.text=@"自定义价格";
        }
        reusableview = headerView;
    }
    reusableview.backgroundColor = RGBA(234, 234, 234, 234);
    return reusableview;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    [self.priceArray removeAllObjects];
    
    
    self.priceRangeCell.leftTextfield.text=@"";
    self.priceRangeCell.rightTextfield.text=@"";
    
    for (WYPriceModel *model in self.dataArray) {
        model.state = NO;
    }
    WYPriceModel *model = self.dataArray[indexPath.row];
    model.state = YES;
    [collectionView reloadData];
    
    if (indexPath.row!=0) {
        [self.priceArray addObject:model];
    }
    
    
    
    NSLog(@"点击了某一个cell");
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
        if (indexPath.section==0) {
            return CGSizeMake((kScreenWidth-50)/4, 40);
        }else{
            return CGSizeMake(kScreenWidth-50, 40);
        }
}


-(UIButton *)sureButton
{
    if (!_sureButton) {
        _sureButton = [[UIButton alloc]init];
        _sureButton.backgroundColor = RGBA(59, 128, 249, 1.0);
        [_sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sureButton setTitle:@"确定" forState:UIControlStateNormal];
         _sureButton.frame = CGRectMake(0, self.view.height/3*2-50, kScreenWidth, 50);
        [_sureButton addTarget:self action:@selector(sureAction) forControlEvents:(UIControlEventTouchUpInside)];
       // _sureButton.hidden=YES;
    }
    
    
    return _sureButton;
}
-(void)sureAction
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeShow" object:nil];
    [self sureActionisTextField];
    [self.view removeFromSuperview];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"priceModel" object:@{@"priceModel":self.priceArray}];
    
    
}
-(void)sureActionisTextField
{
        [self.priceRangeCell.leftTextfield resignFirstResponder];
        [self.priceRangeCell.rightTextfield resignFirstResponder];
        //WYPriceModel;
        if (!self.priceRangeCell.leftTextfield.text.length&&!self.priceRangeCell.rightTextfield.text.length) {
            return;
        }
       [self.priceArray removeAllObjects];
        if (self.priceRangeCell.leftTextfield.text.length&&!self.priceRangeCell.rightTextfield.text.length) {
            
            WYPriceModel *priceModel = [[WYPriceModel alloc]init];
            priceModel.name = [NSString stringWithFormat:@"%@以上",self.priceRangeCell.leftTextfield.text];
            priceModel.minPrice = self.priceRangeCell.leftTextfield.text;
            [self.priceArray addObject:priceModel];
        }
        if (!self.priceRangeCell.leftTextfield.text.length&&self.priceRangeCell.rightTextfield.text.length) {
            
            WYPriceModel *priceModel = [[WYPriceModel alloc]init];
            priceModel.name = [NSString stringWithFormat:@"%@以下",self.priceRangeCell.leftTextfield.text];
            priceModel.maxPrice = self.priceRangeCell.leftTextfield.text;
            [self.priceArray addObject:priceModel];
        }
        if (self.priceRangeCell.leftTextfield.text.length&&self.priceRangeCell.rightTextfield.text.length) {
            WYPriceModel *priceModel = [[WYPriceModel alloc]init];
            priceModel.name = [NSString stringWithFormat:@"%@~%@万",self.priceRangeCell.leftTextfield.text,self.priceRangeCell.rightTextfield.text];
            priceModel.maxPrice = self.priceRangeCell.leftTextfield.text;
            [self.priceArray addObject:priceModel];
        }

}





@end
